package com.reyco1.util
{
	public class ArrayUtil
	{
		public static function addItemAt(data:Array, item:Object, index:uint):void 
		{
			data.splice(index, 0, item);
		}
		
		public static function addItem(data:Array, item:Object):void 
		{
			data.push(item);
		}
		
		public static function addItemsAt(data:Array, items:Array, index:uint):void 
		{
			data.splice.apply(data, [index ,0].concat(items));
		}
		
		public static function addItems(data:Array, items:Array):void 
		{
			addItemsAt(data, items, data.length);
		}
		
		public static function getItemAt(data:Array, index:uint):Object 
		{
			return data[index];
		}
		
		public static function getItemIndex(data:Array, item:Object):int 
		{
			return data.indexOf(item);
		}
		
		public static function removeItemAt(data:Array, index:uint):Object 
		{
			var arr:Array = data.splice(index, 1);
			return arr[0];
		}
		
		public static function removeItem(data:Array, item:Object):Object 
		{
			var index:int = getItemIndex(data, item);
			if (index != -1) 
			{
				return removeItemAt(data, index);
			}
			return null;
		}
		
		public static function removeAll(data:Array):void 
		{
			data = [];
		}
		
		public static function replaceItem(data:Array, newItem:Object, oldItem:Object):Object 
		{
			var index:int = getItemIndex(data, oldItem);
			if (index != -1) 
			{
				return replaceItemAt(data,newItem,index);
			}
			return null;
		}
		
		public static function replaceItemAt(data:Array, newItem:Object,index:uint):Object 
		{
			var arr:Array = [data[index]];
			data[index] = newItem;
			return arr[0];
		}
		
		public static function sort(data:Array, ...sortArgs:Array):* 
		{
			var returnValue:Array = data.sort.apply(data, sortArgs);
			return returnValue;
		}
		
		public static function sortOn(data:Array, fieldName:Object, options:Object = null):* 
		{
			var returnValue:Array = data.sortOn(fieldName, options);
			return returnValue;
		}
		
		public static function getItemsBy(data:Array, key:String, value:*):Array
		{
			return data.filter(Delegate.create(filterItemByType, {key:key, value:value}));
		}	
		
		public static function getItemBy(data:Array, key:String, value:*):*
		{
			return data.filter(Delegate.create(filterItemByType, {key:key, value:value}))[0];
		}	
		
		private static function filterItemByType(item:*, index:Number, array:Array, param:Object = null):Boolean
		{
			return item[param.key] == param.value;
		}
		
		public static function contains(data:Array, item:*):Boolean 
		{
			return (getItemIndex(data, item) > -1);
		}
		
		public static function clone(data:Array):Array 
		{
			return data.concat();
		}
		
		public static function shuffle(array:Array):Array
		{
			var len:Number = array.length; 
			var rand:Number;
			var temp:*;
			for (var i:Number = len-1; i >= 0; i--)
			{ 
				rand = Math.floor(Math.random()*len); 
				temp = array[i]; 
				array[i] = array[rand]; 
				array[rand] = temp; 
			} 
			return array
		}
	}
}